package Control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import Entity.Job_Registration;
import Form.Job_RegistrationForm;
import Form.Job_SearchForm;
import Form.Job_UpdateForm;
import Form.Job_jobRegistrationForm;

public class Job_View_Controller {
	private Job_SearchForm parser;
	private Job_Registration selectedJob = new Job_Registration();

	private ArrayList<Job_Registration> jobList = new ArrayList<Job_Registration>();

	public Job_SearchForm getParser() {
		return parser;
	}

	public void setParser(Job_SearchForm parser) {
		this.parser = parser;
	} 
	
	public Job_Registration getSelectedJob() {
		return selectedJob;
	}

	public void setSelectedJob(Job_Registration selectedJob) {
		this.selectedJob = selectedJob;
	}

	
	public Job_View_Controller(Job_SearchForm parser){ 
		this.setParser(parser);
		
		jobList = Job_Registration.retrieveJob(); 
		generateJob();
	} 
	
	
	public void generateJob(){ 

		getParser().getPanel().removeAll();
		getParser().getPanel().repaint();
		getParser().getPanel().revalidate();

		
		getParser().getPanel().setPreferredSize(new Dimension(900, jobList.size() * 30)); 
		
		for(int i = 0; i < jobList.size(); i++){ 
			final int tempIndex = i;
			JPanel job = new JPanel(); 
			JLabel jobID = new JLabel(jobList.get(i).getJobID());
			JLabel jobName = new JLabel(jobList.get(i).getJobName()); 
			JLabel jobDesc = new JLabel(jobList.get(i).getJobDesc()); 
			JLabel educationCode = new JLabel(jobList.get(i).getEducationCode()); 
			JLabel salary = new JLabel(Double.toString(jobList.get(i).getSalary())); 
			JLabel noSlots = new JLabel(Integer.toString(jobList.get(i).getNoSlots())); 
			JLabel categoryCode = new JLabel(jobList.get(i).getCategoryCode()); 
			JLabel statusCode = new JLabel(jobList.get(i).getJobStatusCode()); 
			
			job.setPreferredSize(new Dimension(900, 30));
			job.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.black));
			FlowLayout layout = (FlowLayout)job.getLayout(); 
			layout.setAlignment(FlowLayout.LEFT); 
			
			
			job.addMouseListener(new MouseListener(){

				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(CommonControls.getUserType() == "Needy"){
						setSelectedJob(jobList.get(tempIndex));
						JDialog registerPanel = new JDialog(parser.getParser(), true);
						registerPanel.add(new Job_jobRegistrationForm(parser));
						
						registerPanel.pack();
						registerPanel.setLocationRelativeTo(parser.getParser());
						registerPanel.setVisible(true); 

						
					}
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				} 
				
				
				
			});
		
			JSeparator s1 = new JSeparator(); 
			
			
			
			jobID.setPreferredSize(new Dimension(100,20));
			job.add(jobID);
			s1 = new JSeparator(); 
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1); 
			s1.setForeground(Color.black);
			
			jobName.setPreferredSize(new Dimension(100,20));
			job.add(jobName);
			s1 = new JSeparator(); 
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1); 
			s1.setForeground(Color.black);
			
			jobDesc.setPreferredSize(new Dimension(100,20));
			job.add(jobDesc); 
			s1 = new JSeparator();
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1); 
			s1.setForeground(Color.black); 
			
			educationCode.setPreferredSize(new Dimension(100,20));
			job.add(educationCode); 
			s1 = new JSeparator();
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1); 
			s1.setForeground(Color.black);
			
			salary.setPreferredSize(new Dimension(50,20));
			job.add(salary); 
			s1 = new JSeparator();
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1); 
			s1.setForeground(Color.black);
			
			noSlots.setPreferredSize(new Dimension(50,20));
			job.add(noSlots); 

			s1 = new JSeparator(); 
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1);
			s1.setForeground(Color.black);
			
			categoryCode.setPreferredSize(new Dimension(150,20));
			job.add(categoryCode); 

			s1 = new JSeparator();
			s1.setPreferredSize(new Dimension(10, 30));
			s1.setOrientation(SwingConstants.VERTICAL);
			job.add(s1); 
			s1.setForeground(Color.black);
			
			statusCode.setPreferredSize(new Dimension(100,20));
			job.add(statusCode);
			
			getParser().getPanel().add(job);
		} 
		getParser().getPanel().repaint();
		getParser().getPanel().revalidate();
	}
	
	public void searchJobName(){ 
		jobList = Job_Registration.searchJobName(getParser().getSearchJobName().getText()); 
		generateJob(); 

	}
	
	public void searchJobReq(){ 
		jobList = Job_Registration.searchEduLvl((String) getParser().getSearchJobReq().getSelectedItem()); 
		generateJob();
	}
	
	public void searchJobCat(){ 
		jobList = Job_Registration.searchJobCat((String) getParser().getSearchJobCat().getSelectedItem());
		generateJob();
	}
}
